﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace hconnect {
    public class Siemens102Util {
        public static string ByteTOstr(byte bytes) {
            if( bytes == 0 ) {
                return "00";//00
            }
            string df = "{0:X}";
            return string.Format(df, bytes);//{0:X}
        }
        ///<summary >short转换字符串</summary>
        public static string ShortTOstr(short shorts) {
            if( shorts == 0 ) {
                return "00 00";//00 00
            }
            byte[] bytes = BitConverter.GetBytes(shorts);
            string df = "{0:X} {1:X}";
            return string.Format(df ,bytes[1] ,bytes[0]); //{0:X} {1:X}
        }
        ///<summary >int转换字符串</summary>
        public static string IntTOstr(int int_0) {
            if( int_0 == 0 ) {
                return "00 00 00 00";//
            }
            byte[] bytes = BitConverter.GetBytes(int_0);
            string df = "{0:X} {1:X} {2:X} {3:X}";
            return string.Format(df, new object[]
            {
                bytes[3],
                bytes[2],
                bytes[1],
                bytes[0]
            });
        }
        ///<summary >浮点转换字符串</summary>
        public static string FloatTOstr(float floats) {
            if( floats == 0f ) {
                return "00 00 00 00";//00 00 00 00
            }
            byte[] bytes = BitConverter.GetBytes(floats);
            string df = "{0:X} {1:X} {2:X} {3:X}";
            return string.Format(df ,new object[]   /*{0:X} {1:X} {2:X} {3:X}*/
			{
                bytes[3],
                bytes[2],
                bytes[1],
                bytes[0]
            });
        }
        ///<param >将字符串转化为byte数组</param>
        ///<summary >空格分隔字符串、转换16进制</summary>
        public static byte[] StrToArraybyte(string str)//字符串转换数组
        {
            string[] array = str.Split(new char[] { ' ' });//以空格分割字符串 并生成数组
            int num = array.Length;
            byte[] array2 = new byte[num];
            for( int i = 0;i < num;i++ ) {
                var str1 = "0x" + array[i];
                array2[i] = Convert.ToByte(str1 ,16); //8 位无符号、16进制转换
            }
            return array2;
        }
    }
}
